﻿using System;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using mki.Backup.Objects.Properties;

namespace mki.Backup.Objects.Dialogs
{
	/// <summary>
	/// Represents the ConnectionStringEditorForm class
	/// </summary>
	public partial class ConnectionStringEditorForm : Form
	{
		#region Fields (1)

		// Fields (1) 

		private const String connectionStringTemplate = "Data Source={0};Initial Catalog={1};User Id={2};Password={3};";

		#endregion Fields

		#region Constructors (1)

		/// <summary>
		/// Initializes a new instance of the <see cref="ConnectionStringEditorForm"/> class.
		/// </summary>
		public ConnectionStringEditorForm()
		{
			InitializeComponent();
			this.DialogResult = DialogResult.None;
			this.saveButton.Click += new EventHandler(saveButtonClick);
		}

		#endregion Constructors

		#region Properties (1)

		// Public Properties (1) 

		/// <summary>
		/// Gets or sets the connection string.
		/// </summary>
		/// <value>The connection string.</value>
		public String ConnectionString
		{
			get { return this.getConnectionString(); }
			set { this.setConnectionString(value); }
		}

		#endregion Properties

		#region Methods (3)

		// Private Methods (3) 

		/// <summary>
		/// Gets the connection string.
		/// </summary>
		/// <returns>returns a object of type String</returns>
		private String getConnectionString()
		{
			if (!String.IsNullOrWhiteSpace(this.serverTextBox.Text) && !String.IsNullOrWhiteSpace(this.databaseTextBox.Text) && !String.IsNullOrWhiteSpace(this.userNameTextBox.Text) && !String.IsNullOrWhiteSpace(this.passwordTextBox.Text))
			{
				return String.Format(connectionStringTemplate, this.serverTextBox.Text, this.databaseTextBox.Text, this.userNameTextBox.Text, this.passwordTextBox.Text);
			}
			return String.Empty;
		}

		/// <summary>
		/// Saves the button click.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void saveButtonClick(object sender, EventArgs e)
		{
			if (String.IsNullOrWhiteSpace(getConnectionString()))
			{
				MessageBox.Show(Resources.ErrorConnectionStringValues, Resources.ErrorHeadline, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}

			this.DialogResult = DialogResult.OK;
		}

		/// <summary>
		/// Sets the connection string.
		/// </summary>
		/// <param name="value">The value.</param>
		private void setConnectionString(String value)
		{
			var regex = new Regex(@"Data Source=(?<server>.*);Initial Catalog=(?<database>.*);User Id=(?<userName>.*);Password=(?<password>.*);", RegexOptions.IgnoreCase);

			var matches = regex.Matches(value);

			if (matches.Count > 0)
			{
				this.serverTextBox.Text = matches[0].Groups["server"].Value;
				this.databaseTextBox.Text = matches[0].Groups["database"].Value;
				this.userNameTextBox.Text = matches[0].Groups["userName"].Value;
				this.passwordTextBox.Text = matches[0].Groups["password"].Value;
			}
		}

		#endregion Methods
	}
}